﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DBA.Common;
using DBA.Data;
using DBA.Data.ADO;
using DBA.Model;
using DBA.Web.UI;
using DBA.Bll;

namespace DBA.Web.Analysis
{
    public partial class EmployeeAnalysis : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["showType"] = UtilsHelper.Decode(Request.Params["type"]);
            if (IsPostBack) return;
            txt_Year.Value = System.DateTime.Now.ToString("yyyy");
            hidYear.Value = txt_Year.Value.Trim();
            BaseCodeDDL.DdlInit(ddlXi, "Xi");//绑定系
            IDictionary<String, String> dict = new Dictionary<string, String>();
            switch (ViewState["showType"].ToString())
            {
                case "0": //查看所有教师信息                    
                    break;
                case "1": //查看当前系的所有教师信息
                    ddlXi.SelectedValue = ServerContext.AdminCurrentUser.Xi_ID.ToString();
                    ddlXi.Enabled = false;
                   
                    break;
                case "2": //查看当前教师自己的信息
                    ddlXi.SelectedValue = ServerContext.AdminCurrentUser.Xi_ID.ToString();
                    ddlXi.Enabled = false;
                    dict.Add("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID.ToString());
                    divExcel.Visible = false;
                    break;
            }
            Bind(1, dict);
        }

        protected void linkBtnSearch_Click(object sender, EventArgs e)
        {
            IDictionary<String, string> dict = new Dictionary<String, string>();
            switch (ViewState["showType"].ToString())
            {              
                case "2": //查看当前教师自己的信息
                    dict.Add("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID.ToString());
                    break;
            }
            dict.Add("UserName", txt_Name.Text.Trim());
            dict.Add("Year", txt_Year.Value.Trim());
            hidYear.Value = txt_Year.Value.Trim();
            ViewState["conditions"] = dict;
            Bind(1, dict);
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            IDictionary<String, String> dict = null;
            if (ViewState["conditions"] != null)
            {
                dict = (IDictionary<String, String>)ViewState["conditions"];
            }
            else
            {
                dict = new Dictionary<string, String>();
                switch (ViewState["showType"].ToString())
                {
                    case "2": //查看当前教师自己的信息
                        dict.Add("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID.ToString());
                        break;
                }
            }
            Bind(e.NewPageIndex, dict);
        }

        private void Bind(int pageIdx, IDictionary<String, String> dict)
        {

            string sql = "GetPagingTeacherWorkload";
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            dal.ManagerParameter.ClearParameter();
            switch (ViewState["showType"].ToString())
            {
                case "0":
                    dal.ManagerParameter.AddNewInParameter("EmployeeID", "");
                    string xiid = ddlXi.SelectedValue == "-1" ? "" : ddlXi.SelectedValue;
                    dal.ManagerParameter.AddNewInParameter("XiID", xiid);
                    break;
                case "1": //查看当前系的所有的服务工作量                   
                    dal.ManagerParameter.AddNewInParameter("XiID", ddlXi.SelectedValue);                  
                    dal.ManagerParameter.AddNewInParameter("EmployeeID", "");
                    break;
                case "2"://查看当前教师自己的服务工作量
                    if (dict.ContainsKey("EmployeeID"))
                    {
                        //sql += " and a.EmployeeID = @EmployeeID";
                        dal.ManagerParameter.AddNewInParameter("EmployeeID", dict["EmployeeID"].ToString());
                        dal.ManagerParameter.AddNewInParameter("XiID", "");
                    }
                    break;
            }
            if (dict != null)
            {
                if (dict.ContainsKey("UserName"))
                {
                    dal.ManagerParameter.AddNewInParameter("UserName", dict["UserName"].ToString());
                }
                else
                {
                    dal.ManagerParameter.AddNewInParameter("UserName", "");
                }
                if (dict.ContainsKey("Year"))
                {
                    dal.ManagerParameter.AddNewInParameter("Year", dict["Year"].ToString());
                }
                else
                {
                    dal.ManagerParameter.AddNewInParameter("Year", System.DateTime.Now.ToString("yyyy"));
                }
                //if (dict.ContainsKey("XiID") && dict["XiID"].ToString()!="-1")
                //{
                //    dal.ManagerParameter.AddNewInParameter("XiID", dict["XiID"].ToString());
                //}
                //else
                //{
                //    dal.ManagerParameter.AddNewInParameter("XiID", "");
                //}
            }
            else
            {
                dal.ManagerParameter.AddNewInParameter("UserName", "");
                dal.ManagerParameter.AddNewInParameter("Year", System.DateTime.Now.ToString("yyyy"));
                dal.ManagerParameter.AddNewInParameter("XiID", "");
            }
            
            dal.ManagerParameter.AddNewInParameter("in_pageIndex", pageIdx);
            dal.ManagerParameter.AddNewInParameter("in_pagesize", AspNetPager1.PageSize);
            dal.ManagerParameter.AddNewInParameter("out_pagecount", 0);
            dal.ManagerParameter.AddNewInParameter("out_recordcount", 0);

            DataSet ds = dal.ExecuteFillDataSet(sql, true);
            int iCount = ConvertHelper.ToInt32(ds.Tables[0].Rows[0][1]);

            replData.DataSource = ds.Tables[1];
            replData.DataBind();
            AspNetPager1.RecordCount = iCount;

        }

        protected void lnkDownload_Click(object sender, EventArgs e)
        {           
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            IDictionary<String, string> dict = new Dictionary<String, string>();
            switch (ViewState["showType"].ToString())
            {
                case "2": //查看当前教师自己的信息
                    dict.Add("EmployeeID", ServerContext.AdminCurrentUser.EmployeeID.ToString());
                    break;
            }
            dict.Add("UserName", txt_Name.Text.Trim());
            dict.Add("Year", txt_Year.Value.Trim());      
            dal.ManagerParameter.ClearParameter();
            if (dict != null)
            {
                if (!VerificationHelper.IsNullOrEmpty(dict["UserName"]))
                {
                    dal.ManagerParameter.AddNewInParameter("UserName", dict["UserName"].ToString());
                }
                else
                {
                    dal.ManagerParameter.AddNewInParameter("UserName", "");
                }
                if (!VerificationHelper.IsNullOrEmpty(dict["Year"]))
                {
                    dal.ManagerParameter.AddNewInParameter("Year", dict["Year"].ToString());
                }
                else
                {
                    dal.ManagerParameter.AddNewInParameter("Year", System.DateTime.Now.ToString("yyyy"));
                }
               
            }
            else
            {
                dal.ManagerParameter.AddNewInParameter("UserName", "");
                dal.ManagerParameter.AddNewInParameter("Year", System.DateTime.Now.ToString("yyyy"));              
            }
            dal.ManagerParameter.AddNewInParameter("XiID", ddlXi.SelectedValue=="-1"?"":ddlXi.SelectedValue);
           
           NPOIHelper.ExportByWeb(dal.ExecuteFillDataSet("GetTeacherWorkload", true).Tables[0],
               UtilsHelper.GetMapPath(UtilsHelper.ServerUrl + "ExcelTemplate//totalTemplate.xls"),
               3, "教师工作量统计" + new Random().Next().ToString() + ".xls");
        }
    }
}